<template>
    <div class='login-container'>
        <div class="login-box">
            <!-- 登录logo -->
            <div class="login-logo">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- 账号输入 -->
            <el-form ref="loginFormRef" :model='loginForm' :rules='loginFormRules' label-width="0px" class="login-from">
                <el-form-item prop="username">
                    <el-input v-model='loginForm.username' prefix-icon="iconfont icon-user"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model='loginForm.password' prefix-icon="iconfont icon-3702mima" type="password"></el-input>
                </el-form-item>
                <el-form-item class="login-btn">
                    <el-button type="primary" @click='login'>登录</el-button>
                    <el-button type="info" @click='resetLoginForm'>重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('账号/密码错误')
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
body,html,.login-container{
    background: rgb(117,119,255);
    width: 100%;
    height: 100%;
}
.login-box{
    width: 600px;
    height: 300px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    border-radius: 6px;
    .login-logo{
        width: 120px;
        height: 120px;
        background: #fff;
        border-radius: 100%;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        padding: 8px;
        box-shadow: 0 0 5px #eee;
        img{
            width: 100%;
            height: 100%;
            background: #eee;
            border-radius: 100%;
        }
    }
    .login-from{
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 20px;
        box-sizing: border-box;
        .login-btn{
            display: flex;
            justify-content: flex-end;
        }
    }
}

</style>
